<?php

namespace App\Listeners;

//use App\Events\AccessEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Log;
use Mail;
use Request;
use Response;

use App\ListenAccess;

/**
 * Class AccessEventListener
 * php artisan make:listener AccessEventListen --event=AccessEvent
 *
 * @package App\Listeners
 */
class AccessEventListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  AccessEvent  $event
     * @return void
     */
    public function handle(AccessEvent $event)
    {
        //
    }

    /**
     * Register the listeners for the subscriber.
     *
     * @param $event
     */
    public function subscribe($event)
    {
        // 监听事件类时, 需要 php artisan make:event AccessEvent 新建改事件类
        // 触发时, new 该事件实例 Event::fire(new App\Event\AccessEvent());
//        $event->listen(
//            'App\Events\AccessEvent',
//            'App\Listeners\AccessEventListener@onAccess'
//        );

        $event->listen(
            'access.anywhere',
            'App\Listeners\AccessEventListener@onAccessAnywhere'
        );

        $event->listen(
            'access.front',
            'App\Listeners\AccessEventListener@onAccessFront'
        );

        $event->listen(
            'access.backend',
            'App\Listeners\AccessEventListener@onAccessBackend'
        );
    }

    /**
     * 监听所有请求, 将请求信息保存至日志和数据库
     */
    public function onAccessAnywhere() {
        // $requestScheme = Request::secure() ? 'https' : 'http';
//        $requestScheme = Request::getScheme();
//        $fullUri = '';
//        $fullUri .= $requestScheme . '://';
//        $fullUri .= Request::server('SERVER_NAME');
//        $fullUri .= Request::server('REQUEST_URI');
        $fullUri = Request::getSchemeAndHttpHost() . Request::getRequestUri();
        $data = [
            'url' => Request::url(),
            'full_uri' => $fullUri,
            'request_scheme' => Request::getScheme(),
            'http_host' => Request::getHttpHost(),
            'path_info' => Request::getPathInfo(),
            'query_string' => Request::server('QUERY_STRING'),
            // 等价于 Request::server('REQUEST_URI')
            // 等价于 app('request')->server->get('REQUEST_URI')
//            'request_uri' => Request::getRequestUri(),
            // 'request_method' => Request::server('REQUEST_METHOD'),
            'request_method' => Request::getMethod(),
            'is_ajax' => Request::ajax(),
            'request_format' => Request::format(),
            'client_ip' => Request::getClientIp(),
            'user_agent' => Request::server('HTTP_USER_AGENT'),
            // 'user_agent' => app('request')->server->get('HTTP_USER_AGENT'),
            'request_time' => Request::server('REQUEST_TIME')
        ];
//        dd($data);
        $listenAccess = new ListenAccess($data);
        $listenAccess->save();
        Log::info('Access "' . Request::getRequestUri() . '" From ' . Request::getClientIp() . '.');
    }

    public function onAccessFront($clientIp) {
//        dd(\Request::getClientIp());
        Log::info('Access Front From ' . $clientIp);
    }

    public function onAccessBackend($clientIp) {
        Log::warning('Access Backend From ' . $clientIp);

        /**
        // 发送邮件
        $data = ['email' => env('MAIL_MINE'), 'name' => '我爱吃小球藻', 'clientIp' => $clientIp];
        Mail::send('emails.access', $data, function($message) use($data) {
            $message->to($data['email'], $data['name'])->subject('Access Backend Form ' . $data['clientIp']);
        });
         */
    }
}
